package top.lixunda.ecommerce.server.good.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import top.lixunda.commerce.common.core.entity.MessageObject;
import top.lixunda.ecommerce.server.business.BaseController;
import top.lixunda.ecommerce.server.business.api.entity.dto.PageDTO;
import top.lixunda.ecommerce.server.good.api.controller.IGoodProduceController;
import top.lixunda.ecommerce.server.good.api.entity.db.GoodProduceDO;
import top.lixunda.ecommerce.server.good.api.entity.dto.GoodProduceInsertDTO;
import top.lixunda.ecommerce.server.good.api.entity.dto.GoodProduceQueryDTO;
import top.lixunda.ecommerce.server.good.api.entity.dto.GoodProduceUpdateDTO;
import top.lixunda.ecommerce.server.good.api.entity.vo.GoodProduceVO;
import top.lixunda.ecommerce.server.good.service.IGoodProduceService;

import java.util.List;

/**
 * 商品描述，放在详情页商品基本信息下方
 *
 * @author lixunda
 * @version time:2020/3/11 12:49
 */
@Slf4j
@RestController
public class GoodProduceController extends BaseController implements IGoodProduceController {

    private final IGoodProduceService goodProduceService;

    @Autowired
    public GoodProduceController(IGoodProduceService goodProduceService) {
        this.goodProduceService = goodProduceService;
    }

    /**
     * 根据id查询商品介绍内容信息
     *
     * @param id 商品介绍内容编号
     * @return 商品介绍内容信息
     */
    @Override
    public MessageObject<GoodProduceVO> queryGoodProduceById(Integer id) {
        GoodProduceDO goodProduceDO = goodProduceService.queryGoodProduceById(id);
        GoodProduceVO vo = GoodProduceVO.fromGoodProduceDO(goodProduceDO);
        return getSuccessMessageObject("获取数据成功", vo);
    }

    /**
     * 批量查询，根据id查询商品介绍内容信息
     *
     * @param ids 商品介绍内容编号
     * @return 商品介绍内容信息
     */
    @Override
    public MessageObject<List<GoodProduceVO>> queryGoodProduceByIds(List<Integer> ids) {
        List<GoodProduceDO> list = goodProduceService.queryGoodProduceByIds(ids);
        List<GoodProduceVO> voList = GoodProduceVO.fromGoodProduceDO(list);
        return getSuccessMessageObject("获取数据成功", voList);
    }

    /**
     * 根据商品id查询商品介绍内容信息
     *
     * @param id 商品编号
     * @return 商品介绍内容信息
     */
    @Override
    public MessageObject<List<GoodProduceVO>> queryGoodProduceByGoodId(Integer id) {
        List<GoodProduceDO> list = goodProduceService.queryGoodProduceByGoodId(id);
        List<GoodProduceVO> voList = GoodProduceVO.fromGoodProduceDO(list);
        return getSuccessMessageObject("获取数据成功", voList);
    }

    /**
     * 条件分页查询商品介绍信息
     *
     * @param pageDTO 条件分页查询数据
     * @return 分页查询数据
     */
    @Override
    public MessageObject<IPage<GoodProduceVO>> queryGoodProduceWrapperPage(PageDTO<GoodProduceDO, GoodProduceQueryDTO> pageDTO) {
        IPage<GoodProduceDO> page = goodProduceService.queryGoodProduceWrapperPage(pageDTO);
        IPage<GoodProduceVO> resultPage = pageConvert(page, GoodProduceVO::fromGoodProduceDO);
        return getSuccessMessageObject("获取数据成功", resultPage);
    }

    /**
     * 插入一条商品介绍内容信息，图片类型专用
     *
     * @param files                图片文件，批量
     * @param goodProduceInsertDTO 插入数据
     * @return 插入后的商品介绍内容信息
     */
    @Override
    public MessageObject<List<GoodProduceVO>> insertGoodProduceWithFile(
            MultipartFile[] files, GoodProduceInsertDTO goodProduceInsertDTO) {
        List<GoodProduceDO> list = goodProduceService.insertGoodProduceWithFile(files, goodProduceInsertDTO);
        List<GoodProduceVO> voList = GoodProduceVO.fromGoodProduceDO(list);
        return getSuccessMessageObject("添加数据成功", voList);
    }

    /**
     * 插入一条商品介绍内容信息，不能插入图片类型的商品介绍信息
     *
     * @param list 插入数据
     * @return 插入后的商品介绍内容信息
     */
    @Override
    public MessageObject<List<GoodProduceVO>> insertGoodProduce(List<GoodProduceInsertDTO> list) {
        List<GoodProduceDO> doList = goodProduceService.insertGoodProduce(list);
        List<GoodProduceVO> voList = GoodProduceVO.fromGoodProduceDO(doList);
        return getSuccessMessageObject("添加数据成功", voList);
    }

    /**
     * 根据商品介绍内容编号更改商品介绍内容信息
     *
     * @param list 更改数据
     * @return 更改后的商品介绍内容信息
     */
    @Override
    public MessageObject<List<GoodProduceVO>> changeGoodProduceById(List<GoodProduceUpdateDTO> list) {
        List<GoodProduceDO> doList = goodProduceService.changeGoodProduceById(list);
        List<GoodProduceVO> voList = GoodProduceVO.fromGoodProduceDO(doList);
        return getSuccessMessageObject("修改数据成功", voList);
    }

    /**
     * 根据商品介绍内容编号移除商品介绍内容信息
     *
     * @param ids 商品介绍内容编号
     * @return 是否移除成功
     */
    @Override
    public MessageObject<Boolean> removeGoodProduceByIds(List<Integer> ids) {
        boolean success = goodProduceService.removeGoodProduceByIds(ids);
        return success ? getSuccessMessageObject("移除数据成功", true) :
                getErrorMessageObject("移除数据失败", false);
    }
}
